﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.Reflection.Emit;

namespace Entity
{
    public class SMDBContext : DbContext
    {
        public SMDBContext(DbContextOptions<SMDBContext> opts) : base(opts)
        {

        }

        /// <summary>
        /// 用户信息
        /// </summary>
        public DbSet<User> EntityUser { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            User use = new User();
            use.FirstName = "1213";
            use.LastName = "123";

            modelBuilder.Entity<User>().HasData([
                new User{
                    FirstName="zhangsan",
                    LastName="san",

                }

                ]);
        }

        ///// <summary>
        ///// 配置数据库信息
        ///// </summary>
        ///// <param name="optionsBuilder"></param>
        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder
        //        .UseMySql(_connectionString,new MySqlServerVersion(new Version(9,2,0)));

        //}

        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    modelBuilder
        //        .UseCollation("utf8mb4_german2_ci")
        //        .HasCharSet("utf8mb4");
        //}
    }

    public class SMDBContextFactory : IDesignTimeDbContextFactory<SMDBContext>
    {
        public SMDBContext CreateDbContext(string[] args)
        {
            var optionBuilder = new DbContextOptionsBuilder<SMDBContext>();
            optionBuilder.UseMySql("server=localhost;userid=root;password=123456;database=AIYDB;",
                new MySqlServerVersion(new Version(8, 0, 41))
                );

            return new SMDBContext(optionBuilder.Options);
        }
    }
}
